import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class Server 
{
	public static ArrayList<Socket> lstClientSocket;	
	
	public static ServerSocket server ;
	
	
	
	
	public static void SendDataToClient(String data , Socket  theclient) throws IOException
	{
		try
		{
			PrintWriter out = new PrintWriter(theclient.getOutputStream(),true); 	
		
		try 
		{ 	
			out.println(data); 
				
		}catch(Exception ex)
		{
			System.out.println("Loi khi send data to client_1."+ ex.toString());
		}
		out.flush(); 		
		//out.close();		
		//theclient.close();		
		
		}
		catch(Exception ex)
		{
			System.out.println("Loi khi send data to client_2."+ ex.toString());
		}
		
	}
	
	public static  void Listen() throws InterruptedException
	{
		try {			
			
			System.out.println("Waiting for a Client connect...");
			
			Socket ss = server.accept(); //synchronous			
			lstClientSocket.add(ss);			
			System.out.println("count accept " + lstClientSocket.size() +" name: "+  ss.toString());			
			HandClient(ss);// nhan data tu client.
			while(true)
			{
				Thread.sleep(1000);
				String data = "datafrom server";
				
				SendDataToClient(data, ss);
				
				
			}
						
		}
		catch (IOException e) {
			
			System.out.println("Loi khi listen client."+ e.toString());
		
		}
		
	}
	

	
	
	public static void HandClient(Socket ss)
	{
		try
		{						
			ReciveClient rc = new ReciveClient(ss);			
			
		}
		catch(Exception e)
		{
			System.out.println("There're some error");
		}
		
		
	}
	
	public static  void SendClient(Socket ss)
	{
		try
		{
			SendClient sl = new SendClient(ss);
			
		}
		catch(Exception ex)
		{
			
		}
	
		
	}
	
	public static void main(String arg[])
	{
	
		try
		{
			lstClientSocket = new ArrayList<Socket>();
			
			server = new ServerSocket(3211);
			
			
			do
			{	//System.out.print("a");
				Listen();
				//System.out.print("end a");
			}
			while (true);
			
		}
		catch(Exception e)
		{
			System.out.println("There're some error");
		}
	}
}


class ReciveClient extends Server implements Runnable { 
	
	private Socket theclient;  
	
	public ReciveClient (Socket theclient) { 
		this.theclient = theclient; 		
		Thread t = new Thread(this); 
		t.start(); 
	} 
	
	public void run() { 
		
		try 
		{ 
			PrintWriter out = new PrintWriter(theclient.getOutputStream(),true); 
			
			BufferedReader in = new BufferedReader(new InputStreamReader(theclient.getInputStream()));
		
			
			String inputLine; 
			while((inputLine=in.readLine()) != null) {	
								
				System.out.println("From Client:"+inputLine);					
				if (inputLine.equals("Bye")) 
					break; 
			} 		
				
		this.lstClientSocket.remove(theclient);		
		out.flush();	
		out.close();
		in.close(); 	
		theclient.close(); 		
		} 
		catch (IOException e) { 
			e.printStackTrace(); 
		} 
	} 
	
}


class SendClient implements Runnable { 
	
	private Socket theclient; 
	ServerSocket serverSocket = null; 
	
	public SendClient(Socket theclient) { 
		this.theclient = theclient; 		
		Thread t = new Thread(this); 
		t.start(); 
	} 
	
	public void run() { 
		try 
		{ 
			PrintWriter out = new PrintWriter(theclient.getOutputStream(),true); 
			BufferedReader in = new BufferedReader(new InputStreamReader(theclient.getInputStream())); 
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
			
			String inputLine, outputLine; 
			while(true) { 
				out.println(); 
				
				//System.out.println("From Client:"+inputLine); 
				System.out.println("to client: ");
				outputLine=input.readLine(); 
				System.out.println("data send: " + outputLine);
				out.println(outputLine); 
				
				if (outputLine.equals("Bye")) 
					break; 
			} 
			
		out.flush(); 		
		out.close(); 
		in.close(); 
		theclient.close(); 
		serverSocket.close(); 
		
		} 
		catch (IOException e) { 
			e.printStackTrace(); 
		} 
	} 
	
}





